<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\AttractionManagement\Service\Attraction;

use Plugin\Attractions\AttractionManagement\Repository\Attraction\AttractionRepository;
use App\Service\IService;

/**
 * 景区表服务类
 */
final class AttractionService extends IService
{
    public function __construct(
        protected readonly AttractionRepository $repository
    ) {}

    /**
     * 重写page方法，处理cover_id字段
     */
    public function page(array $params, int $page = 1, int $pageSize = 10): array
    {
        $result = parent::page($params, $page, $pageSize);
        
        if (isset($result['list']) && is_array($result['list'])) {
            $appHost = env('APP_HOST');
            $httpPort = env('HTTP_PORT');
            $baseUrl = $appHost . ':' . $httpPort . '/uploads/';
            
            foreach ($result['list'] as &$item) {
                if (isset($item['cover_id']) && !empty($item['cover_id'])) {
                    if (!str_starts_with($item['cover_id'], 'http://') && !str_starts_with($item['cover_id'], 'https://')) {
                        $item['cover_url'] = $baseUrl . ltrim($item['cover_id'], '/');
                    } else {
                        $item['cover_url'] = $item['cover_id'];
                    }
                }
            }
        }
        
        return $result;
    }
}